package Arcos::JobQueue::Handler::TellAFriend;

use strict;
use warnings;

use base qw(Arcos::JobQueue::Handler);

use Arcos::SiteMail;
use Params::Validate qw(validate ARRAYREF);

=pod

=head1 NAME

Arcos::JobQueue::TellAFriend - Job handler for tell-a-friend e-mails

=head1 SYNOPSIS

  use Arcos::JobQueue::TellAFriend
  Arcos::JobQueue::TellAFriend->handler( from_email => 'foo@foo.com',
                                         from_name  => 'Bob',
                                         url        => 'http://www.foo.com/',
                                         friends    => [ { name  => 'Joe',
                                                           email => 'joe@joe.com' },
                                                         { name  => 'Jim',
                                                           email => 'jim@jim.com' },
                                                         ...
                                                       ]
                                       );

=head1 DESCRIPTION

This module is a handler for use with L<Arcos::JobQueue>. It uses L<MIME::Lite> to send
emails via SMTP to the people specified in the C<friends> parameter to tell them about
a URL on an Arcos site. This serves as the backend processor for L<Arcos::Form::TellAFriend>. 

=head1 INTERFACE

=over 4

=item C<handler>

This class method will send an email. The following parameters are required.

=over 8

=item C<from_email> - The email 'from' address.

=item C<from_name> - The name of the sender.

=item C<url> - The URL to send.

=item C<friends> - A reference to an array of hashrefs containing the recipients. Each
hashref should contains the keys C<name> and C<email>. 

=back

=back

=cut

sub handler {
    my $class = shift;
    my %params = validate(@_,
                          {template   => 1,
                           from_email => 1,
                           from_name  => 1,
                           url        => 1,
                           friends    => {type => ARRAYREF},
                          });

    Arcos::SiteMail->send(
          template      => $params{template},
          from_email    => $params{from_email},
          from_name     => $params{from_name},
          subject       => 'A message from ' . $params{from_name},
          recipients    => $params{friends},
          template_data => {url => $params{url}, message => $params{message}},
    );

    return {result => 1};
}

1;
